#!/bin/sh
#############################
#   Entry point functuons   #
#############################

############# Capabilities ##################
prefix_list="$prefix_list $DMROOT.Capabilities."
entry_execute_method_list="$entry_execute_method_list entry_execute_method_root_Capabilities"

UCI_GET_VARSTATE="/sbin/uci -q ${UCI_CONFIG_DIR:+-c $UCI_CONFIG_DIR} -P /var/state get"
UCI_SET_VARSTATE="/sbin/uci -q ${UCI_CONFIG_DIR:+-c $UCI_CONFIG_DIR} -P /var/state set"

entry_execute_method_root_Capabilities() {
	case "$1" in ""|"$DMROOT."|"$DMROOT.Capabilities."*)
		common_execute_method_obj "$DMROOT.Capabilities." "0"
                common_execute_method_obj "$DMROOT.Capabilities.PerformanceDiagnostic." "0"
 	        common_execute_method_param "$DMROOT.Capabilities.PerformanceDiagnostic.DownloadTransports" "1" "tr143_get easycwmp.@local[0].Cap_Download HTTP" ""
		common_execute_method_param "$DMROOT.Capabilities.PerformanceDiagnostic.UploadTransports" "1" "tr143_get easycwmp.@local[0].Cap_Upload HTTP" ""
                return 0;
                ;;
        esac
        return $E_INVALID_PARAMETER_NAME;

}

############# DownloadDiagnostics ##################
prefix_list="$prefix_list $DMROOT.DownloadDiagnostics."
entry_execute_method_list="$entry_execute_method_list entry_execute_method_root_DownloadDiagnostics"

entry_execute_method_root_DownloadDiagnostics() {
	case "$1" in ""|"$DMROOT."|"$DMROOT.DownloadDiagnostics."*)
		common_execute_method_obj "$DMROOT.DownloadDiagnostics." "0"
		common_execute_method_param "$DMROOT.DownloadDiagnostics.DiagnosticsState" "1" "tr143_get easycwmp.@local[0].Dl_DiagnosticsState None" "tr143_dl_set_diagnostic_state"
		common_execute_method_param "$DMROOT.DownloadDiagnostics.DownloadURL" "1" "tr143_get easycwmp.@local[0].Dl_DownloadURL" "tr143_dl_set easycwmp.@local[0].Dl_DownloadURL"
		common_execute_method_param "$DMROOT.DownloadDiagnostics.Interface" "1" "tr143_get easycwmp.@local[0].Dl_Interface" "tr143_dl_set easycwmp.@local[0].Dl_Interface"
		common_execute_method_param "$DMROOT.DownloadDiagnostics.EthernetPriority" "1" "tr143_get easycwmp.@local[0].Dl_EthernetPriority 0" "tr143_dl_set easycwmp.@local[0].Dl_EthernetPriority"
		common_execute_method_param "$DMROOT.DownloadDiagnostics.DSCP" "1" "tr143_get easycwmp.@local[0].Dl_DSCP 0" "tr143_dl_set easycwmp.@local[0].Dl_DSCP"
		common_execute_method_param "$DMROOT.DownloadDiagnostics.TCPOpenRequestTime" "0" "tr143_get easycwmp.@local[0].Dl_TCPOpenRequestTime" "tr143_dl_set easycwmp.@local[0].Dl_TCPOpenRequestTime" "xsd:dateTime"
		common_execute_method_param "$DMROOT.DownloadDiagnostics.TCPOpenResponseTime" "0" "tr143_get easycwmp.@local[0].Dl_TCPOpenResponseTime" "tr143_dl_set easycwmp.@local[0].Dl_TCPOpenResponseTime" "xsd:dateTime"
		common_execute_method_param "$DMROOT.DownloadDiagnostics.BOMTime" "0" "tr143_get easycwmp.@local[0].Dl_BOMTime" "tr143_dl_set easycwmp.@local[0].Dl_BOMTime" "xsd:dateTime"
		common_execute_method_param "$DMROOT.DownloadDiagnostics.ROMTime" "0" "tr143_get easycwmp.@local[0].Dl_ROMTime" "tr143_dl_set easycwmp.@local[0].Dl_ROMTime" "xsd:dateTime"
		common_execute_method_param "$DMROOT.DownloadDiagnostics.EOMTime" "0" "tr143_get easycwmp.@local[0].Dl_EOMTime" "tr143_dl_set easycwmp.@local[0].Dl_EOMTime" "xsd:dateTime"
		common_execute_method_param "$DMROOT.DownloadDiagnostics.TestBytesReceived" "0" "tr143_get easycwmp.@local[0].Dl_TestBytesReceived 0" "tr143_dl_set easycwmp.@local[0].Dl_TestBytesReceived" "xsd:unsignedInt"
		common_execute_method_param "$DMROOT.DownloadDiagnostics.TotalBytesReceived" "0" "tr143_get easycwmp.@local[0].Dl_TotalBytesReceived 0" "tr143_dl_set easycwmp.@local[0].Dl_TotalBytesReceived" "xsd:unsignedInt"
		return 0;
		;;
	esac
	return $E_INVALID_PARAMETER_NAME;
}

############# DownloadDiagnostics ##################
prefix_list="$prefix_list $DMROOT.UploadDiagnostics."
entry_execute_method_list="$entry_execute_method_list entry_execute_method_root_UploadDiagnostics"

entry_execute_method_root_UploadDiagnostics() {
        case "$1" in ""|"$DMROOT."|"$DMROOT.UploadDiagnostics."*)
                common_execute_method_obj "$DMROOT.UploadDiagnostics." "0"
                common_execute_method_param "$DMROOT.UploadDiagnostics.DiagnosticsState" "1" "tr143_get easycwmp.@local[0].Ul_DiagnosticsState None" "tr143_ul_set_diagnostic_state"
                common_execute_method_param "$DMROOT.UploadDiagnostics.UploadURL" "1" "tr143_get easycwmp.@local[0].Ul_UploadURL" "tr143_ul_set easycwmp.@local[0].Ul_UploadURL"
                common_execute_method_param "$DMROOT.UploadDiagnostics.Interface" "1" "tr143_get easycwmp.@local[0].Dl_Interface" "tr143_ul_set easycwmp.@local[0].Dl_Interface"
                common_execute_method_param "$DMROOT.UploadDiagnostics.EthernetPriority" "1" "tr143_get easycwmp.@local[0].Dl_EthernetPriority 0" "tr143_ul_set easycwmp.@local[0].Dl_EthernetPriority"
                common_execute_method_param "$DMROOT.UploadDiagnostics.DSCP" "1" "tr143_get easycwmp.@local[0].Dl_DSCP 0" "tr143_ul_set easycwmp.@local[0].Dl_DSCP"
                common_execute_method_param "$DMROOT.UploadDiagnostics.TCPOpenRequestTime" "0" "tr143_get easycwmp.@local[0].Ul_TCPOpenRequestTime" "tr143_ul_set easycwmp.@local[0].Ul_TCPOpenRequestTime" "xsd:dateTime"
                common_execute_method_param "$DMROOT.UploadDiagnostics.TCPOpenResponseTime" "0" "tr143_get easycwmp.@local[0].Ul_TCPOpenResponseTime" "tr143_ul_set easycwmp.@local[0].Ul_TCPOpenResponseTime" "xsd:dateTime"
                common_execute_method_param "$DMROOT.UploadDiagnostics.BOMTime" "0" "tr143_get easycwmp.@local[0].Ul_BOMTime" "tr143_ul_set easycwmp.@local[0].Ul_BOMTime" "xsd:dateTime"
                common_execute_method_param "$DMROOT.UploadDiagnostics.ROMTime" "0" "tr143_get easycwmp.@local[0].Ul_ROMTime" "tr143_ul_set easycwmp.@local[0].Ul_ROMTime" "xsd:dateTime"
                common_execute_method_param "$DMROOT.UploadDiagnostics.EOMTime" "0" "tr143_get easycwmp.@local[0].Ul_EOMTime" "tr143_ul_set easycwmp.@local[0].Ul_EOMTime" "xsd:dateTime"
		common_execute_method_param "$DMROOT.UploadDiagnostics.TestBytesSent" "0" "tr143_get easycwmp.@local[0].Ul_TestBytesSent 0" "tr143_ul_set easycwmp.@local[0].Ul_TestBytesSent" "xsd:unsignedInt"
                common_execute_method_param "$DMROOT.UploadDiagnostics.TotalBytesSent" "0" "tr143_get easycwmp.@local[0].Ul_TotalBytesSent 0" "tr143_ul_set easycwmp.@local[0].Ul_TotalBytesSent" "xsd:unsignedInt"
                return 0;
                ;;
        esac
        return $E_INVALID_PARAMETER_NAME;
}

############# UDPEchoConfig ##################
prefix_list="$prefix_list $DMROOT.UDPEchoConfig."
entry_execute_method_list="$entry_execute_method_list entry_execute_method_root_UDPEchoConfig"

entry_execute_method_root_UDPEchoConfig() {
        case "$1" in ""|"$DMROOT."|"$DMROOT.UDPEchoConfig."*)
                common_execute_method_obj "$DMROOT.UDPEchoConfig." "0"
                common_execute_method_param "$DMROOT.UDPEchoConfig.Enable" "1" "tr143_get easycwmp.@local[0].UEcho_Enable 0" "tr143_do_udpecho"
                common_execute_method_param "$DMROOT.UDPEchoConfig.Interface" "1" "tr143_get easycwmp.@local[0].UEcho_Interface" "tr143_udpecho_set easycwmp.@local[0].UEcho_Interface"
                common_execute_method_param "$DMROOT.UDPEchoConfig.SourceIPAddress" "1" "tr143_get easycwmp.@local[0].UEcho_SourceIPAddress" "tr143_udpecho_set easycwmp.@local[0].UEcho_SourceIPAddress"
                common_execute_method_param "$DMROOT.UDPEchoConfig.UDPPort" "1" "tr143_get easycwmp.@local[0].UEcho_UDPPort 0" "tr143_udpecho_set easycwmp.@local[0].UEcho_UDPPort" "xsd:unsignedInt"
                common_execute_method_param "$DMROOT.UDPEchoConfig.EchoPlusEnabled" "1" "tr143_get easycwmp.@local[0].UEcho_EchoPlusEnabled" "tr143_udpecho_set easycwmp.@local[0].UEcho_EchoPlusEnabled" "xsd:boolean"
                common_execute_method_param "$DMROOT.UDPEchoConfig.EchoPlusSupported" "0" "tr143_get easycwmp.@local[0].UEcho_EchoPlusSupported 0" "tr143_udpecho_set easycwmp.@local[0].UEcho_EchoPlusSupported" "xsd:boolean"
                common_execute_method_param "$DMROOT.UDPEchoConfig.PacketsReceived" "0" "tr143_get easycwmp.@local[0].UEcho_PacketsReceived" "tr143_udpecho_set easycwmp.@local[0].UEcho_PacketsReceived" "xsd:unsignedInt"
                common_execute_method_param "$DMROOT.UDPEchoConfig.PacketsResponded" "0" "tr143_get easycwmp.@local[0].UEcho_PacketsResponded" "tr143_udpecho_set easycwmp.@local[0].UEcho_PacketsResponded" "xsd:unsignedInt"
                common_execute_method_param "$DMROOT.UDPEchoConfig.BytesReceived" "0" "tr143_get easycwmp.@local[0].UEcho_BytesReceived" "tr143_udpecho_set easycwmp.@local[0].UEcho_BytesReceived" "xsd:unsignedInt"
                common_execute_method_param "$DMROOT.UDPEchoConfig.BytesResponded" "0" "tr143_get easycwmp.@local[0].UEcho_BytesResponded" "tr143_udpecho_set easycwmp.@local[0].UEcho_BytesResponded" "xsd:unsignedInt"
                common_execute_method_param "$DMROOT.UDPEchoConfig.TimeFirstPacketReceived" "0" "tr143_get easycwmp.@local[0].UEcho_TimeFirstPacketReceived 0" "tr143_udpecho_set easycwmp.@local[0].UEcho_TimeFirstPacketReceived" "xsd:dateTime"
 	        common_execute_method_param "$DMROOT.UDPEchoConfig.TimeLastPacketReceived" "0" "tr143_get easycwmp.@local[0].UEcho_TimeLastPacketReceived 0" "tr143_udpecho_set easycwmp.@local[0].UEcho_TimeLastPacketReceived" "xsd:dateTime"
                return 0;
                ;;
        esac
        return $E_INVALID_PARAMETER_NAME;
}

#######################################
#   Data model parameters functions   #
#######################################

tr143_get() {
	local val=`$UCI_GET_VARSTATE $1`
	echo ${val:-$2}
}

tr143_dl_set() {
	tr143_dl_stop_diagnostic
	if [ "`$UCI_GET_VARSTATE easycwmp.@local[0].Dl_DiagnosticsState`" != "Requested" ]; then
		$UCI_SET_VARSTATE easycwmp.@local[0].Dl_DiagnosticsState=None
	fi
	$UCI_SET_VARSTATE $1=$2
	return 0
}

tr143_dl_set_diagnostic_state() {
	local val="$1"
	case $val in 
		Requested)
			tr143_dl_stop_diagnostic
			$UCI_SET_VARSTATE easycwmp.@local[0].Dl_DiagnosticsState=Requested
			common_execute_command_in_apply_service "/bin/sh $FUNCTION_PATH/tr143_download_launch run &"
			return 0
		;;
	esac
	return $E_INVALID_PARAMETER_VALUE;
}

tr143_dl_stop_diagnostic() {
	local pids=`ps | grep tr143_download_launch | grep -v grep | awk '{print $1}'`
	if [ -n "$pids" ]; then
		kill -9 $pids
		$UCI_SET_VARSTATE easycwmp.@local[0].Dl_DiagnosticsState=None
	fi
}

tr143_ul_set() {
        tr143_ul_stop_diagnostic
        if [ "`$UCI_GET_VARSTATE easycwmp.@local[0].Ul_DiagnosticsState`" != "Requested" ]; then
                $UCI_SET_VARSTATE easycwmp.@local[0].Ul_DiagnosticsState=None
        fi
        $UCI_SET_VARSTATE $1=$2
        return 0
}

tr143_ul_stop_diagnostic() {
        local pids=`ps | grep tr143_upload_launch | grep -v grep | awk '{print $1}'`
        if [ -n "$pids" ]; then
                kill -9 $pids
                $UCI_SET_VARSTATE easycwmp.@local[0].Ul_DiagnosticsState=None
        fi
}

tr143_ul_set_diagnostic_state() {
        local val="$1"
        case $val in
                Requested)
                        tr143_ul_stop_diagnostic
                        $UCI_SET_VARSTATE easycwmp.@local[0].Ul_DiagnosticsState=Requested
                        common_execute_command_in_apply_service "/bin/sh $FUNCTION_PATH/tr143_upload_launch run &"
                        return 0
                ;;
        esac
        return $E_INVALID_PARAMETER_VALUE;
}

tr143_udpecho_stop_diagnostic() {
	common_execute_command_in_apply_service "/bin/sh $FUNCTION_PATH/tr143_udpecho_launch stop"
}

tr143_udpecho_set() {
        $UCI_SET_VARSTATE $1=$2
        return 0
}

tr143_do_udpecho() {
        local val="$1"
        case $val in
                0)
                        tr143_udpecho_stop_diagnostic
			return 0
		;;
		1)
			tr143_udpecho_stop_diagnostic
			common_execute_command_in_apply_service "/bin/sh $FUNCTION_PATH/tr143_udpecho_launch run &"
                        return 0
                ;;
        esac
        return $E_INVALID_PARAMETER_VALUE;
}

